package com.cloud.bc.controller.pc;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.controller.BaseController;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcContacapplys;
import com.cloud.bc.model.entity.BcSingleContacts;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.LikeRequest;
import com.cloud.bc.model.response.MomenSingleResponse;
import com.cloud.bc.model.response.MyContactResponse;
import com.cloud.bc.service.BcContacapplysService;
import com.cloud.bc.service.BcSingleContactsService;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUsersService;
import com.cloud.bc.util.JiaoYouPageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

//PC联系方式
@Controller
@AllArgsConstructor
@RequestMapping("center/contact")
public class PcCenterContactController extends BaseController {
    private final BcSingleContactsService bcSingleContactsService;
    private final BcUsersService bcUsersService;
    private final BcContacapplysService bcContacapplysService;
    private final BcSinglesService bcSinglesService;

    /**
     * 联系方式
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "联系方式-用户中心", "pc/center/contact/index");
        view.addObject("bodyCss", "page_manage page_manage_contact");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSingleContacts bcSingleContacts = bcSingleContactsService.selectOne(new BcSingleContacts().setUserId(userInfo.getId()));
        view.addObject("data", bcSingleContacts);
        return view;
    }

    /**
     * 我申请的
     */
    @GetMapping("mycontact")
    public ModelAndView mycontact(HttpServletRequest request, HttpServletResponse response,
                                  LikeRequest req) {
        ModelAndView view = baseView(request, "我申请的-用户中心", "pc/center/contact/mycontact");
        view.addObject("bodyCss", "page_manage page_manage_contact");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
//        type 2 申请看我，3我申请看的
        Page p = new Page();
        p.setCurrent(Convert.toInt(req.getPage(), 0) <= 0 ? 1 : req.getPage());
//        p.setSize(10);
        BcContacapplys bcContacapplys = new BcContacapplys();
//           3我申请看的
        bcContacapplys.setUserId(userInfo.getId());

        IPage<BcContacapplys> bcContacapplysIPage = bcContacapplysService.selectPage(p, bcContacapplys);
        List<BcContacapplys> records = bcContacapplysIPage.getRecords();
        Set<Integer> userIds = records.stream().map(t -> t.getUserId()).collect(Collectors.toSet());
        List<MyContactResponse> contactResponseData = bcContacapplysService.getContactResponseData(records, userIds);
        view.addObject("contactList", contactResponseData);
        JiaoYouPageHelper jiaoYouPageHelper = new JiaoYouPageHelper();
        view.addObject("page", jiaoYouPageHelper.getBcPageHTML(p, request));
        return view;
    }

    /**
     * 申请我的
     */
    @GetMapping("contactme")
    public ModelAndView contactme(HttpServletRequest request, HttpServletResponse response,
                                  LikeRequest req) {
        ModelAndView view = baseView(request, "申请我的-用户中心", "pc/center/contact/contactme");
        view.addObject("bodyCss", "page_manage page_manage_contact");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
//        type 2 申请看我，3我申请看的
        Page p = new Page();
        p.setCurrent(Convert.toInt(req.getPage(), 0) <= 0 ? 1 : req.getPage());
//        p.setSize(1);
        BcContacapplys bcContacapplys = new BcContacapplys();
//           2 申请看我
        bcContacapplys.setTargetId(userInfo.getId());
        IPage<BcContacapplys> bcContacapplysIPage = bcContacapplysService.selectPage(p, bcContacapplys);
        List<BcContacapplys> records = bcContacapplysIPage.getRecords();
        Set<Integer> userIds = records.stream().map(t -> t.getTargetId()).collect(Collectors.toSet());
        List<MyContactResponse> contactResponseData = bcContacapplysService.getContactResponseData(records, userIds);
        view.addObject("contactList", contactResponseData);
        JiaoYouPageHelper jiaoYouPageHelper = new JiaoYouPageHelper();
        view.addObject("page", jiaoYouPageHelper.getBcPageHTML(p, request));
        return view;
    }
}
